package com.hk.commons.poi.excel.read.sax.v03.handler;

import com.hk.commons.poi.excel.read.sax.v03.XlsReadContext;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.Record;

/**
 * 单元格为公式类型
 *
 * @author Kevin
 */
public class FormulaRecordHandler implements XlsRecordHandler {

    @Override
    public void processRecord(Record poiRecord, XlsReadContext readContext) {
        FormulaRecord formulaRecord = (FormulaRecord) poiRecord;
        String value = null;
        if (readContext.isOutputFormulaValue()) {
            if (Double.isNaN(formulaRecord.getValue())) {
                readContext.setOutputNextStringRecord(true);
                readContext.setNextColumn(formulaRecord.getColumn());
            } else {
                value = readContext.getFormatListener().formatNumberDateCell(formulaRecord);
            }
        } else {
            value = HSSFFormulaParser.toFormulaString(readContext.getStubWorkbook(), formulaRecord.getParsedExpression());
        }
        readContext.getColumnValueMap().put(formulaRecord.getColumn(), value);
    }
}
